<template>
	<div class="race-index">
		<el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="170px" class="demo-ruleForm">
			<el-form-item prop="waterPoolId" label="所属池子">
				<el-select v-model="ruleForm.waterPoolId" style="width: 300px;" placeholder="请选择池子">
					<el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item prop="policyType" label="策略类型">
				<el-select v-model="ruleForm.policyType" style="width: 300px;" placeholder="请选择策略类型">
					<el-option v-for="item in policyTypeOptions" :key="item.value" :label="item.label"
						:value="item.value">
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item prop="indicator" label="指标名称">
				<el-input style="width: 300px;" v-model="ruleForm.indicator" placeholder="请输入指标名称"></el-input>
			</el-form-item>
			<el-form-item prop="designValue" label="设计值">
				<el-input style="width: 300px;" v-model="ruleForm.designValue" placeholder="请输入设计值"></el-input>
			</el-form-item>
			<el-form-item prop="measuredValue" label="实测值">
				<el-input style="width: 300px;" v-model="ruleForm.measuredValue" placeholder="请输入实测值"></el-input>
			</el-form-item>
			<el-form-item prop="time" label="时间点">
				<el-date-picker style="width: 300px;" v-model="ruleForm.time" format="yyyy-MM-dd HH:mm" value-format="yyyy-MM-dd HH:mm" type="datetime" placeholder="选择日期时间">
				</el-date-picker>
			</el-form-item>
			<el-form-item prop="unit" label="单位名称">
				<el-input style="width: 300px;" v-model="ruleForm.unit" placeholder="请输入单位名称"></el-input>
			</el-form-item>
		</el-form>
		<div style="text-align: center;margin-top: 10px;">
			<el-button type="primary" size="mini" @click="go2Submit">{{ruleForm.id ? "立即更新" : "立即创建"}}</el-button>
			<el-button size="mini" @click="go2Cancel">取消</el-button>
		</div>
	</div>
</template>

<script>
	import {
		create,
		update
	} from '@/api/yongyue/influentQualityDay.js'
	import {
		fetchList as fetchPoolList
	} from '@/api/yongyue/waterPool.js'
	import {
		fetchList as fetchPolicyTypeList
	} from '@/api/yongyue/policyType.js'
	import {
		getToken
	} from '@/utils/auth'

	export default {
		components: {},
		props: {
			originData: {
				type: Object,
				default () {
					return {}
				}
			}
		},
		data() {
			return {
				dialogVisible: false,
				ruleForm: {
					waterPoolId: undefined,
					policyType: "",
					indicator: '',
					designValue: "",
					measuredValue: "",
					time: "",
					unit: ""
				},
				rules: {
					waterPoolId: [{
						required: true,
						message: '请选择所属池子',
						trigger: 'blur'
					}],
					policyType: [{
						required: true,
						message: '请选择策略类型',
						trigger: 'blur'
					}],
					indicator: [{
						required: true,
						message: '请输入指标名称',
						trigger: 'blur'
					}],
					designValue: [{
						required: true,
						message: '请输入设计值',
						trigger: 'blur'
					}],
					measuredValue: [{
						required: true,
						message: '请输入实测值',
						trigger: 'blur'
					}],
					unit: [{
						required: true,
						message: '请输入单位',
						trigger: 'blur'
					}],
					time: [{
						required: true,
						message: '请输入时间',
						trigger: 'blur'
					}],
				},
				loading: "",
				policyTypeOptions: [],
				options: []
			}
		},
		mounted() {
			this.go2Query()
			this.go2QueryPolicyType()
			if (Object.keys(this.originData).length > 0) {
				this.ruleForm = {
					...this.originData
				}
			}
		},
		methods: {
			go2Query() {
				const loading = this.$loading({
					lock: true,
					text: '加载中',
					spinner: 'el-icon-loading',
					background: 'rgba(0, 0, 0, 0.7)'
				})
				const params = {
					page_no: 1,
					page_size: 10000
				}
				fetchPoolList(params).then((res) => {
					loading.close()
					if (res.code === 200) {
						const message = res.data
						this.options = message.items.map(item => {
							return {
								label: item.poolName,
								value: item.id
							}
						})
					}
				}).catch((error) => {
					loading.close()
					console.log(error)
				})
			},
			go2QueryPolicyType() {
				const loading = this.$loading({
					lock: true,
					text: '加载中',
					spinner: 'el-icon-loading',
					background: 'rgba(0, 0, 0, 0.7)'
				})
				const params = {
					page_no: 1,
					page_size: 10000
				}
				fetchPolicyTypeList(params).then((res) => {
					loading.close()
					if (res.code === 200) {
						const message = res.data
						this.policyTypeOptions = message.items.map(item => {
							return {
								label: item.name,
								value: item.id
							}
						})
					}
				}).catch((error) => {
					loading.close()
					console.log(error)
				})
			},
			go2Submit() {
				this.$refs['ruleForm'].validate((valid) => {
					if (valid) {
						const loading = this.$loading({
							lock: true,
							text: '提交中',
							spinner: 'el-icon-loading',
							background: 'rgba(0, 0, 0, 0.7)'
						});
						if (this.ruleForm.id) {
							// 更新操作
							let req = {
								...this.ruleForm
							}
							update(this.ruleForm.id, req).then((res) => {
								if (res.code === 200) {
									const message = res.message
									this.$message.success('更新成功')
									this.go2Cancel()
								} else {
									this.$message.error(res.msg || '更新失败')
								}
							}).catch((error) => {
								console.log(error)
							}).finally(() => {
								loading.close()
							})
						} else {
							// 添加操作
							let req = {
								...this.ruleForm
							}
							create(req).then((res) => {
								console.log("aaaaaa", res)
								if (res.code === 200) {
									const message = res.message
									this.$message.success('新增成功')
									this.go2Cancel()
								} else {
									this.$message.error(res.msg || '新增失败')
								}
							}).catch((error) => {
								console.log(error)
							}).finally(() => {
								loading.close()
							})
						}
					} else {
						console.log('error submit!!')
						return false
					}
				})
			},
			go2Cancel() {
				this.$emit('close')
			},
			resetForm(formName) {
				this.$refs[formName].resetFields();
			}
		},
	}
</script>

<style lang="scss">
	.race-index {
		padding: 15px;

		.avatar-uploader .el-upload {
			border: 1px dashed #d9d9d9;
			border-radius: 6px;
			cursor: pointer;
			position: relative;
			overflow: hidden;
		}

		.avatar-uploader .el-upload:hover {
			border-color: #409EFF;
		}

		.avatar-uploader-icon {
			font-size: 28px;
			color: #8c939d;
			width: 178px;
			height: 178px;
			line-height: 178px;
			text-align: center;
		}

		.avatar {
			width: 178px;
			height: 178px;
			display: block;
		}
	}
</style>